﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyPublisher.Data;
using System.Configuration;

namespace MyPublisher.Web
{
    public partial class Download : System.Web.UI.Page
    {
        public const string USERNAME_COLUMN = "User Name";
        public const string ID_COLUMN = "ID";
        public const string PROVIDER_COLUMN = "Data Provider";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(DocumentId))
                {
                    Response.Clear();
                    string fileName = String.Format("{0}.zip",DocumentId);

                    Response.ClearHeaders();
                    Response.Buffer = false;
                    Response.ContentType = "application/octet-stream";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    Response.WriteFile(String.Format(@"{0}\{1}", ConfigurationManager.AppSettings["pubdir"], fileName));
                    Response.CacheControl = "no-cache"; 
                    Response.End();
                }
                else
                {
                    Publication pub = new Publication();
                    DocumentGrid.DataSource = pub.GetAll();
                    DocumentGrid.DataBind();

                    SearchColumns.Items.Add(USERNAME_COLUMN);
                    SearchColumns.Items.Add(ID_COLUMN);
                    SearchColumns.Items.Add(PROVIDER_COLUMN);
                    SearchColumns.SelectedIndex = 0;
                }
            }
        }

        protected void SearchButton_Click(object sender, ImageClickEventArgs e)
        {
            string searchText = SearchTextBox.Text;
            if (searchText == null)
                searchText = string.Empty;
            searchText = searchText.Trim();

            Publication pub = new Publication();
            if (String.IsNullOrEmpty(searchText))
            {
                DocumentGrid.DataSource = pub.GetAll();
                DocumentGrid.DataBind();
                return;
            }
            switch (SearchColumns.SelectedValue)
            {
                case USERNAME_COLUMN:
                    {
                        DocumentGrid.DataSource = pub.GetByUerName(searchText);
                        DocumentGrid.DataBind();
                        break;
                    }
                case ID_COLUMN:
                    {
                        try
                        {
                            Guid id = new Guid(searchText);
                            DocumentGrid.DataSource = pub.GetById(id);
                            DocumentGrid.DataBind();
                        }
                        catch { }
                        break;
                    }
                case PROVIDER_COLUMN:
                    {
                        DocumentGrid.DataSource = pub.GetByProvider(searchText);
                        DocumentGrid.DataBind();
                        break;
                    }
                default:
                    break;
            }
        }

        protected string DocumentId { get { return Request["p"];} }
    }
}
